/*
 * Created on Dec 21, 2004
 */
package org.vectrics.recruiter.document;

import java.util.Collection;
import java.util.Iterator;
import org.apache.log4j.Logger;
//import org.hibernate.Session;
import org.vectrics.batch.Batch;
import org.vectrics.common.util.DateSync;
import org.vectrics.domain.Domain;
import org.vectrics.hibernate.ThreadProperties;
//import org.vectrics.hibernate.HibernateSessionStore;
//import org.vectrics.hibernate.TransactionToken;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.user.RecruitUserProvider;
import org.vectrics.recruiter.user.Resume;

/**
 * @author mmoore
 */
public class CandidateDocumentIndexingBatch extends Batch {
	public static final String BATCH_TYPE_CODE = "resume-index";
	private transient Logger log = Logger.getLogger(this.getClass());
	//private static boolean working = false;

	public CandidateDocumentIndexingBatch(Domain domain) {
		super(domain);
	}
	
	protected String getBatchTypeCode() {
		return(BATCH_TYPE_CODE);
	}
	
	protected String getDescription() {
		return("Index resumes that need indexing");
	}

	protected boolean persistRunResults() {
		return(false);
	}

	protected boolean logOnlyErrors() {
		return(false);
	}
	
	protected boolean executeForEachDomain() {
		return(false);
	}
	
	protected void execute(Domain domain) {
		try {
			if (ThreadProperties.isTransactionInProgress()) {
				ThreadProperties.forcedCommitAndNewTransaction();
			} else {
				ThreadProperties.beginTransaction();
			}
			int count = 0;
			//working = true;
			log.debug("Running batch job: Resume Indexing Batch");
			
			boolean done = false;
			while (!done) {
				Collection documentsWithoutIndexing = RecruitServiceLocator.getRecruitUserService().findDocumentsForIndexing(domain, 0, 10);
				if (documentsWithoutIndexing.size() == 0) {
					done = true;
				}
				if (log.isDebugEnabled())
					log.debug("Resumes to index, count = " + documentsWithoutIndexing.size());
				
				Iterator iter = documentsWithoutIndexing.iterator();
				while ((iter.hasNext()) && (!getTerminated())) {
					CandidateDocument document = (CandidateDocument)iter.next();
					try {
						log.info("Indexing document: " + document.getId());
						if (log.isDebugEnabled())
							log.debug("Indexing document for person: " + document.getPerson() 
									+ "  Id = " + document.getId());
						RecruitServiceLocator.getRecruitUserService().writeDocumentToScanIndex(document);
						count++;
						ThreadProperties.forcedCommit();
					} catch (Exception ex) {
						log.info("exception in execute() - batch id = " + this.getId());
						ThreadProperties.rollbackTransaction();
						log.error("Exception indexing document "+ document.getId(), ex);
						logError("Exception indexing resume "+ document.getId() + ": " + ex.getMessage());
					}
				}
			}
			log.debug("Finished Running batch job: Document" +
					" Indexing Batch - Number of resumes indexed = " + count);
			this.log("Finished Running batch job: Document Indexing" +
					" Batch - Number of resumes indexed = " + count);
		} finally {
			//working = false;
		}
	}

}
